package com.cn.xiaonuo.common.tools;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @BelongsProject: slb-reports-java
 * @Author: likui
 * @CreateTime: 2022-11-19  11:20
 * @Description: resttemplate工具类
 */
@Component
public class RestTemplateUtil {
    @Autowired
    RestTemplate restTemplate;

    public Map post(String url, String method, String params, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("service",method);
        headers.add("authorization",token);
        HttpEntity<String> request = new HttpEntity<String>(params, headers);
        Map returnJson = restTemplate.postForObject(url, request, Map.class);
        return returnJson;
    }

    public String postString(String url, String method, String params, String token) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("service",method);
        headers.add("authorization",token);
        HttpEntity<String> request = new HttpEntity<String>(params, headers);
        String returnJson = restTemplate.postForObject(url, request, String.class);
        return returnJson;
    }
}
